#pragma once

#include <unordered_map>
#include <ftp_user.h>
#include <mutex>
#include <memory>
#include <atomic>

constexpr int MAX_LOGIN_COUNT = 16;

// todo: UserManager的操作需要加锁，不加锁可能会有多个线程同时调用同个函数导致的竞争问题

class UserManager {
public:
	UserManager(const UserManager&) = delete;
	UserManager(UserManager&&) = delete;
	UserManager& operator=(const UserManager&) = delete;
	UserManager& operator=(UserManager&&) = delete;

	std::shared_ptr<FTPUser> getUser(const std::string& username);

	bool addUser(const std::shared_ptr<FTPUser>& user);
	
	bool exist(const std::string& username);
	
	bool userLogin(const std::string& username, const std::string& password);

	bool userQuit(const std::string& username);

	void removeUser(const std::string& username);

	static UserManager& instance();
private:
	UserManager() = default;
	~UserManager() = default;
private:
	std::mutex userCntLock_;
	std::unordered_map<std::string, std::atomic<int>> userLoginCntMap_;
	std::unordered_map<std::string, std::shared_ptr<FTPUser>> users_;
};